package org.droidplanner.android.zz;

import android.app.Dialog;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import org.droidplanner.android.R;


/**
 * @author ZhaoZheng
 * @description: dialog基类封装
 * @date :2024/3/14
 */
public class BaseDialog extends Dialog {

    /**
     * 设置样式 样式设置失败搁置
     *
     * @param style
     */
    public void setDialogStyle(int style) {
        getContext().getTheme().applyStyle(style, true);

    }

    public BaseDialog( Context context, int layout) {
        super(context, R.style.dialog_style);
//        super(context, 0);
        setHideNavigationBar();
        setCustomView(layout);
        setCanceledOnTouchOutside(false);
    }


    public void setCustomView(int layoutResId) {
        setContentView(layoutResId);
    }

    public void setHideNavigationBar(){
        /**
         *  隐藏标题栏
         *  设置全屏显示
         *  如果需要隐藏虚拟导航栏（底部系统导航栏）
         */
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);

        /**
         *    解决弹出框时候底部导航栏弹出
         */
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
        getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
            @Override
            public void onSystemUiVisibilityChange(int visibility) {
                int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
                        //布局位于状态栏下方
                        View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
                        //全屏
                        View.SYSTEM_UI_FLAG_FULLSCREEN |
                        //隐藏导航栏
                        View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
                        View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
                uiOptions |= 0x00001000;
                getWindow().getDecorView().setSystemUiVisibility(uiOptions);
            }
        });
    }

    /**
     * 设置全屏
     */
    public void setFullScreen() {
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

    }

    /**
     * 设置自适应
     */
    public void setScreenAuto() {
        getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

    }

    /**
     * 设置大小
     *
     * @param width
     * @param height
     */
    public void setScreenSize(int width, int height) {
        getWindow().setLayout(width, height);
    }

    /**
     * 设置宽高比例 float 0.6
     *
     * @param widthPercentage
     * @param heightPercentage
     */
    public void setScreenFloat(float widthPercentage, float heightPercentage) {
        WindowManager.LayoutParams params = getWindow().getAttributes();
        DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
        params.width = (int) (displayMetrics.widthPixels * widthPercentage);
        params.height = (int) (displayMetrics.heightPixels * heightPercentage);
        getWindow().setAttributes(params);

    }


}